package day8.homework;

//b.通过线程模拟对同一个变量进行更改,
//   A线程睡眠10毫秒，对变量加1，B线程睡眠20毫秒对变量加1，持续3秒
//   查看多次运行的结果是否一致
public class DemoB {
    public static void main(String[] args) {
        new Demo("DemoA").start();
        new Demo("B").start();
    }

}

class Demo extends Thread {
    static int i;

    public Demo(String name) {
        super(name);
    }

    @Override
    public void run() {
        try {
            long start = System.currentTimeMillis();
            String name = Thread.currentThread().getName();
            while (true) {
                long end = System.currentTimeMillis();
                if (end - start >= 3000) break;
                if ("DemoA".equals(name)) {
                    Thread.sleep(10);
                    i++;
                } else {
                    Thread.sleep(20);
                    i++;
                }
                System.out.println(name + "进行i变量更新:" + i);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
